"use strict"
//查询个人详细信息
const mvc = require("cosjs.mvc");
exports.role = async function () {
    const idType = this.get('idType', 'int');
    const id = this.get('id', 'string');
    const sid = this.get('sid', 'int');
    const guid = this.get('guid', 'string');

    let role_mode = mvc.model.mongo('role', sid);
    let bag_mode = mvc.model.mongo('item', sid);
    let daily_mode = mvc.model.mongo('daily', sid);
    let record_mode = mvc.model.mongo('record', sid);
    let task_mode = mvc.model.mongo('task', sid);
    let active_mode = mvc.model.mongo('active', sid);

    let role_query = {};
    let item_query = {}
    switch (idType) {
        case 0:
            {
                //UID
                role_query['_id'] = id;
                item_query['uid'] = id;

            }
            break;
        case 1:
            {
                //name
                role_query['name'] = id;
                let option = { multi: false, fields: ['_id'] };
                let r = await role_mode.find(role_query, option);
                if (!r) {
                    return this.error('查无此人');
                }
                item_query['uid'] = r['_id'];
            }
            break;
        case 2:
            {
                //GUID
                role_query['guid'] = id;
                let option = { multi: false, fields: ['_id'] };
                let r = await role_mode.find(role_query, option);
                if (!r) {
                    return this.error('查无此人');
                }
                item_query['uid'] = r['_id'];
                //item_query['guid']=id;
            }
            break;
        case 3:
            {
                //openID
                //后台查好guid带过来
                role_query['guid'] = guid;
                let option = { multi: false, fields: ['_id'] };
                let r = await role_mode.find(role_query, option);
                if (!r) {
                    return this.error('查无此人');
                }
                item_query['uid'] = r['_id'];
            }
            break;
        case 4:
            {
                //MyGod

            }
            break;
    }

    let _role = await role_mode.find(role_query);
    let _item = await bag_mode.find(item_query, { multi: true });
    let _daily = await daily_mode.find(item_query, { multi: true });
    let _record = await record_mode.find(item_query, { multi: true });
    let _task = await task_mode.find(item_query, { multi: true });
    let _active = await active_mode.find(item_query, { multi: true });
    return { role: _role, item: _item, daily: _daily, record: _record, task: _task, active: _active };
}

//复制到996服
exports.copy996 = async function () {
    const uid = this.get('uid', 'string');
    const sid = this.get('sid', 'int');

    if (sid == 996) {
        return `复制996的数据做咩野啊？`;
    }

    //先删后增


    //role
    let role_mode_from = mvc.model.mongo('role', sid);
    let role_mode_to = mvc.model.mongo('role', 996);
    let role = await role_mode_from.find({ '_id': uid });
    await role_mode_to.remove({ '_id': uid });
    await role_mode_to.insert(role);



    //bag
    let bag_mode_from = mvc.model.mongo('item', sid);
    let bag_mode_to = mvc.model.mongo('item', 996);
    let item = await bag_mode_from.find({ 'uid': uid }, { multi: true, dataType: "array" });
    await bag_mode_to.remove({ 'uid': uid }, { multi: true });
    if (item.length > 0) {
        await bag_mode_to.insert(item, { multi: true });
    }




    //daily
    let daily_mode_from = mvc.model.mongo('daily', sid);
    let daily_mode_to = mvc.model.mongo('daily', 996);
    let daily = await daily_mode_from.find({ 'uid': uid }, { multi: true, dataType: "array" });
    await daily_mode_to.remove({ 'uid': uid }, { multi: true });
    if (daily.length > 0) {
        await daily_mode_to.insert(daily, { multi: true });
    }



    //record
    let record_mode_from = mvc.model.mongo('record', sid);
    let record_mode_to = mvc.model.mongo('record', 996);
    let record = await record_mode_from.find({ 'uid': uid }, { multi: true, dataType: "array" });
    await record_mode_to.remove({ 'uid': uid }, { multi: true });
    if (record.length > 0) {
        await record_mode_to.insert(record, { multi: true });
    }




    //task
    let task_mode_from = mvc.model.mongo('task', sid);
    let task_mode_to = mvc.model.mongo('task', 996);
    let task = await task_mode_from.find({ 'uid': uid }, { multi: true, dataType: "array" });
    await task_mode_to.remove({ 'uid': uid }, { multi: true });
    if (task.length > 0) {
        await task_mode_to.insert(task, { multi: true });
    }





    //active
    let active_mode_from = mvc.model.mongo('active', sid);
    let active_mode_to = mvc.model.mongo('active', 996);
    let active = await active_mode_from.find({ 'uid': uid }, { multi: true, dataType: "array" });
    await active_mode_to.remove({ 'uid': uid }, { multi: true });
    if (active.length > 0) {
        await active_mode_to.insert(active, { multi: true });
    }




    return '导入成功';
}

